/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "peripheralsmanagementmodel.h"
#include "invokers/invokerfactory.h"
#include "gsettingkey.h"
#include "common.h"

PeripheralsManagementModel::PeripheralsManagementModel(QObject *parent)
    : QObject(parent)
    , m_rootMonitorDBusInter(nullptr)
    , m_peripheralsDBusInter(nullptr)
    , m_peripheralsRevertDialog(nullptr)
{
    m_rootMonitorDBusInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.MonitorNetFlow",
                                                                         "/com/deepin/defender/MonitorNetFlow",
                                                                         "com.deepin.defender.MonitorNetFlow",
                                                                         ConnectType::SYSTEM, this);
    m_rootMonitorDBusInter->Connect("SysSettingValueChanged", this, SLOT(onCurrentEngineValueChanged(QString, QDBusVariant)));

    m_peripheralsDBusInter = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.peripherals",
                                                                         "/com/deepin/defender/peripherals",
                                                                         "com.deepin.defender.peripherals",
                                                                         ConnectType::SYSTEM, this);
    // 连接等保结果返回槽
    m_peripheralsDBusInter->Connect("SendSetPeripheralsPermssonResult", this, SLOT(acceptSetPeripheralsPermssonResult(int, bool, int)));
}

// 外设管控状态设置
void PeripheralsManagementModel::setPeripheralsStatus(SettingType type, bool open)
{
    int nUsbType = int(type);
    DBUS_NOBLOCK_INVOKE(m_peripheralsDBusInter, "SetPeripheralsPermsson", nUsbType, open);
}

void PeripheralsManagementModel::acceptSetPeripheralsPermssonResult(int type, bool open, int result)
{
    m_isFlag = false;
    if (type == SettingType::USBManage && open) {
        if (m_peripheralsRevertDialog == nullptr) {
            m_peripheralsRevertDialog = new PeripheralsRevertDialog();
            connect(m_peripheralsRevertDialog, SIGNAL(onDialogClose()), this,SLOT(peripheralsRevertDialogClose()));
            connect(m_peripheralsRevertDialog, SIGNAL(sendPeripheralsRevert()), this, SLOT(peripheralsRevertDialogSendPeripheralsRevert()));

            connect(m_peripheralsRevertDialog, SIGNAL(closed()), this, SLOT(peripheralsRevertDialogClosed()));
            m_peripheralsRevertDialog->setAttribute(Qt::WA_DeleteOnClose);
            m_peripheralsRevertDialog->setOnButtonClickedClose(true);
            m_peripheralsRevertDialog->show();
        }
    }
}

bool PeripheralsManagementModel::getPeripheralsKey(const QString &key)
{
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_rootMonitorDBusInter, "GetSysSettingValue", key);
    GET_MESSAGE_VALUE(QDBusVariant, valueDBusVar, msg);
    bool status = valueDBusVar.variant().toBool();
    return status;
}

void PeripheralsManagementModel::setPeripheralsKey(const QString &key, bool status)
{
    // 装载到QDBusVariant
    QDBusVariant dbusVar(status);
    // 装载到QVariant
    QVariant var;
    var.setValue(dbusVar);
    DBUS_NOBLOCK_INVOKE(m_rootMonitorDBusInter, "SetSysSettingValue", key, var);
}

void PeripheralsManagementModel::onCurrentEngineValueChanged(const QString &key, const QDBusVariant &value)
{
    if (PERIPHERALS_MANAGE_USB_STATUS == key) {
        bool usbStatus = value.variant().toBool();
        Q_EMIT synPeripheralsStatus(PERIPHERALS_MANAGE_USB_STATUS, usbStatus);
    }

    if (PERIPHERALS_MANAGE_CAMERA_STATUS == key) {
        bool cameraStatus = value.variant().toBool();
        Q_EMIT synPeripheralsStatus(PERIPHERALS_MANAGE_CAMERA_STATUS, cameraStatus);
    }

    if (PERIPHERALS_MANAGE_PRINTER_STATUS == key) {
        bool printerStatus = value.variant().toBool();
        Q_EMIT synPeripheralsStatus(PERIPHERALS_MANAGE_PRINTER_STATUS, printerStatus);
    }
}

void PeripheralsManagementModel::peripheralsRevertDialogClose()
{
    m_peripheralsRevertDialog = nullptr;
}

void PeripheralsManagementModel::peripheralsRevertDialogSendPeripheralsRevert()
{
    m_isFlag = true;
}

void PeripheralsManagementModel::peripheralsRevertDialogClosed()
{
    if (m_isFlag) {
        Q_EMIT notifyPeripheralsRevert();
    }
}
